package cn.z2huo.demo.log.log4j2;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;

import java.util.Arrays;

/**
 * <p>
 *
 * @author z2huo
 */
@Slf4j
@SpringBootApplication
public class Lo4j2Application {

    public void hello() {
        log.info("hello world");
        log.info("hello world");
        log.info("hello world");
        log.info("hello world");
        log.info("hello world");
        log.info("hello world");
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(Lo4j2Application.class, args);

        int length = applicationContext.getBeanDefinitionNames().length;

        log.trace("Spring boot启动初始化了 {} 个 Bean", length);
        log.debug("Spring boot启动初始化了 {} 个 Bean", length);
        log.info("Spring boot启动初始化了 {} 个 Bean", length);
        log.warn("Spring boot启动初始化了 {} 个 Bean", length);
        log.error("Spring boot启动初始化了 {} 个 Bean", length);

//        try {
//            int i = 10 / 0;
//        } catch (Exception e) {
//            log.error("异常消息格式化：用户名 = {}， 用户代码 = {}", RandomStringUtils.randomAlphanumeric(16), RandomStringUtils.randomNumeric(8), e);
//        }

        ConfigurableEnvironment environment = applicationContext.getEnvironment();

        System.out.println();
        System.out.println("===== system env =====");
        environment.getSystemEnvironment().forEach((k, v) -> {
            System.out.println(k + " = " + v);
        });

        System.out.println();
        System.out.println("===== system properties =====");
        environment.getSystemProperties().forEach((k, v) -> {
            System.out.println(k + " = " + v);
        });

        System.out.println();
        System.out.println("===== property sources =====");
        MutablePropertySources propertySources = environment.getPropertySources();
        propertySources.forEach(propertySource -> {
            System.out.println(propertySource.getName());
        });
        System.out.println();
        PropertySource<?> configurationProperties = propertySources.get("configurationProperties");
        System.out.println(configurationProperties.getProperty("spring.application.name"));
        System.out.println(configurationProperties.getProperty("LOGGED_APPLICATION_NAME"));
        System.out.println();

        System.out.println();
        System.out.println(environment.getProperty("spring.application.name"));
        System.out.println(environment.getProperty("LOGGED_APPLICATION_NAME"));

        System.out.println();
        System.out.println("===== profiles =====");
        System.out.println(Arrays.toString(environment.getDefaultProfiles()));
        System.out.println(Arrays.toString(environment.getActiveProfiles()));

    }

}
